﻿Imports System.Data
Imports MyGeneration.dOOdads

Public Class rc_Cobros

    Private _MessageError As Exception = Nothing
    Public Property MessageError() As Exception
        Get
            Return _MessageError
        End Get
        Set(value As Exception)
            _MessageError = value
        End Set
    End Property
    Private log__1 As New Class_RC_Controller.rc_Mantenimiento()

#Region "Cobros"

    Public Function get_Rango_Cobro(conn As String) As String
        Try

            Dim rango As New Class_RC_Data.Class_RC.RANGODOCUMENTOS(conn)
            Dim numero As String = ""
            rango.Where.TipoDocumento.Value = "RECIBO"
            rango.Where.Habilitado.Value = True

            If rango.Query.Load Then
                If rango.DefaultView.Table.Rows.Count > 0 Then

                    numero = rango.Prefijo.ToString() + rango.RangoActual.ToString().PadLeft(4, "0")
                    rango.RangoActual += 1
                    rango.Save()

                    Return numero
                Else
                    Return ""
                End If

            Else
                Return ""

            End If

        Catch ex As Exception
            MessageError = ex
            Return ""
        End Try

    End Function

    Public Function Insert_Cobros(conn As String, fechaCobro As DateTime, codCliente As Integer, nroRecibo As String, total As Decimal,
                                  estadoRecibo As String, codCobrador As Integer, codUsuario As Integer) As Integer

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim cobro As New Class_RC_Data.Class_RC.COBROS(conn)

            cobro.AddNew()
            cobro.FechaCobro = fechaCobro
            cobro.CodCliente = codCliente

            If nroRecibo = "" Then
                cobro.NroRecibo = get_Rango_Cobro(conn)
            Else
                cobro.NroRecibo = nroRecibo
            End If

            cobro.Total = total
            cobro.EstadRecibo = estadoRecibo
            cobro.CodCobrador = codCobrador
            cobro.CodUsuario = codUsuario
            cobro.FechaActualizacion = DateTime.Now
            cobro.Save()
            log__1.Insert_Log(conn, "Inserto registro, codigo: " & cobro.CodCobro, codUsuario, 13, cobro.DefaultView.Table)
            tx.CommitTransaction()

            Return cobro.CodCobro

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return 0
        End Try

    End Function

    Public Function Update_Cobros(conn As String, codCobro As Integer,
                                  fechaCobro As DateTime, codCliente As Integer, nroRecibo As String, total As Decimal,
                                  estadoRecibo As String, codCobrador As Integer, codUsuario As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim cobro As New Class_RC_Data.Class_RC.COBROS(conn)
            cobro.Where.CodCobro.Value = codCobro

            If cobro.Query.Load Then
                log__1.Insert_Log(conn, "Actualiza registro anterior, codigo: " & codCobro, codUsuario, 2, cobro.DefaultView.Table)
                cobro.FechaCobro = fechaCobro
                cobro.CodCliente = codCliente
                'cobro.NroRecibo = nroRecibo
                cobro.Total = total
                cobro.EstadRecibo = estadoRecibo
                cobro.CodCobrador = codCobrador
                cobro.CodUsuario = codUsuario
                cobro.FechaActualizacion = DateTime.Now
                cobro.Save()

            End If
            log__1.Insert_Log(conn, "Actualiza registro editado, codigo: " & codCobro, codUsuario, 2, cobro.DefaultView.Table)
            tx.CommitTransaction()
            Return True
            'Registro editado
        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function Delete_Cobros(ByVal conn As String, ByVal codCobro As Integer, ByVal codusuario As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try

            tx.BeginTransaction()

            Dim cobro As New Class_RC_Data.Class_RC.COBROS(conn)
            cobro.Where.CodCobro.Value = codCobro

            If cobro.Query.Load Then

                Dim det As New Class_RC_Data.Class_RC.DETALLECOBROS(conn)
                det.Where.CodCobro.Value = codCobro
                If det.Query.Load() Then
                    For i As Integer = 0 To (det.DefaultView.Table.Rows.Count - 1)
                        If Delete_Cobros_Detalle_NonTransacc(conn, det.DefaultView.Table.Rows(i)("CodCobroDetalle")) = False Then
                            Throw New Exception("Error al borrar detalles")

                        End If

                    Next

                End If
                log__1.Insert_Log(conn, "Elimino registro, codigo: " & cobro.CodCobro, codusuario, 15, cobro.DefaultView.Table)
                cobro.MarkAsDeleted()
                cobro.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function get_Cobros_All(conn As String, codCobro As Integer, fechaInicio As DateTime,
                                   fechaFin As DateTime, nroRecibo As String, cliente As String,
                                   estado As String, ruc As String,
                                   cobrador As String) As DataTable
        Try

            Dim vCobros As New Class_RC_Data.Class_RC.VistaCobros(conn)

            If codCobro > 0 Then
                vCobros.Where.CodCobro.Value = codCobro
            End If

            If cobrador <> "" Then
                vCobros.Where.Cobrador.Value = cobrador
            End If

            If nroRecibo <> "" Then
                vCobros.Where.NroRecibo.Value = "%" & nroRecibo & "%"
                vCobros.Where.NroRecibo.Operator = WhereParameter.Operand.Like
            Else
                vCobros.Where.FechaCobro.BetweenBeginValue = New DateTime(fechaInicio.Year, fechaInicio.Month, fechaInicio.Day, 0, 0, 0)
                vCobros.Where.FechaCobro.BetweenEndValue = New DateTime(fechaFin.Year, fechaFin.Month, fechaFin.Day, 23, 59, 59)
                vCobros.Where.FechaCobro.Operator = WhereParameter.Operand.Between
            End If

            If estado <> "" Then
                vCobros.Where.EstadoCobro.Value = estado
            End If

            If cliente <> "" Then
                vCobros.Where.Cliente.Value = "%" + cliente + "%"
                vCobros.Where.Cliente.Operator = WhereParameter.Operand.Like
            End If

            If ruc <> "" Then
                vCobros.Where.Ruc.Value = "%" + ruc + "%"
                vCobros.Where.Ruc.Operator = WhereParameter.Operand.Like
            End If

            If vCobros.Query.Load Then
                If vCobros.DefaultView.Table.Rows.Count > 0 Then
                    Return vCobros.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function get_Cobros_Facturas_All(conn As String, codCobro As Integer, fechaInicio As DateTime,
                                   fechaFin As DateTime, nroRecibo As String, cliente As String,
                                   estado As String, ruc As String,
                                   cobrador As String) As DataTable
        Try

            Dim vCobros As New Class_RC_Data.Class_RC.VistaCobros_Facturas(conn)

            If codCobro > 0 Then
                vCobros.Where.CodCobro.Value = codCobro
            End If

            If cobrador <> "" Then
                vCobros.Where.Cobrador.Value = cobrador
            End If

            If nroRecibo <> "" Then
                vCobros.Where.NroRecibo.Value = "%" & nroRecibo & "%"
                vCobros.Where.NroRecibo.Operator = WhereParameter.Operand.Like
            Else
                vCobros.Where.FechaCobro.BetweenBeginValue = New DateTime(fechaInicio.Year, fechaInicio.Month, fechaInicio.Day, 0, 0, 0)
                vCobros.Where.FechaCobro.BetweenEndValue = New DateTime(fechaFin.Year, fechaFin.Month, fechaFin.Day, 23, 59, 59)
                vCobros.Where.FechaCobro.Operator = WhereParameter.Operand.Between
            End If

            If estado <> "" Then
                vCobros.Where.EstadoCobro.Value = estado
            End If

            If cliente <> "" Then
                vCobros.Where.Cliente.Value = "%" + cliente + "%"
                vCobros.Where.Cliente.Operator = WhereParameter.Operand.Like
            End If

            If ruc <> "" Then
                vCobros.Where.Ruc.Value = "%" + ruc + "%"
                vCobros.Where.Ruc.Operator = WhereParameter.Operand.Like
            End If

            If vCobros.Query.Load Then
                If vCobros.DefaultView.Table.Rows.Count > 0 Then
                    Return vCobros.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function get_Cobros_ByID(conn As String, codCobro As Integer) As DataTable
        Try

            Dim vCobros As New Class_RC_Data.Class_RC.VistaCobros(conn)

            If codCobro > 0 Then
                vCobros.Where.CodCobro.Value = codCobro
            End If

            If vCobros.Query.Load Then
                If vCobros.DefaultView.Table.Rows.Count > 0 Then
                    Return vCobros.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function Insert_Cobros_Detalle(conn As String, codCobro As Integer, codVenta As Integer, monto As Decimal, codFormaPago As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim detalle As New Class_RC_Data.Class_RC.DETALLECOBROS(conn)

            detalle.AddNew()
            detalle.CodCobro = codCobro
            detalle.CodVenta = codVenta
            detalle.Monto = monto
            detalle.CodFormaPago = codFormaPago
            detalle.Save()

            'se marca la venta con estado aprobada para evitar la edicion futura de la misma ya que posee un cobro
            Dim fac As New Class_RC_Data.Class_RC.VENTAS(conn)
            fac.Where.CodVenta.Value = codVenta

            If fac.Query.Load Then
                fac.TotalVentaSaldo -= monto
                If fac.TotalVentaSaldo > 0 Then
                    fac.EstadoVenta = "AMORTIZADA"
                Else
                    fac.EstadoVenta = "CANCELADA"
                End If

                fac.Save()
            End If

            tx.CommitTransaction()

            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function Delete_Cobros_Detalle(conn As String, codCobroDetalle As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim detalle As New Class_RC_Data.Class_RC.DETALLECOBROS(conn)
            detalle.Where.CodCobroDetalle.Value = codCobroDetalle

            If detalle.Query.Load Then
                'se marca la factura con estado en proceso pues se anula o borra el cobro asociado.
                'se verifica antes que no posea la venta otro cobro
                Dim detalleControlFac As New Class_RC_Data.Class_RC.DETALLECOBROS(conn)
                detalleControlFac.Where.CodVenta.Value = detalle.CodVenta
                detalleControlFac.Where.CodCobro.Value = detalle.CodCobro
                detalleControlFac.Where.CodCobro.Operator = WhereParameter.Operand.NotEqual

                If Not detalleControlFac.Query.Load Then
                    Dim fac As New Class_RC_Data.Class_RC.VENTAS(conn)
                    fac.Where.CodVenta.Value = detalle.CodVenta
                    If fac.Query.Load Then
                        fac.TotalVentaSaldo += detalle.Monto
                        If fac.TotalVentaSaldo = fac.TotalVenta Then
                            fac.EstadoVenta = "EN PROCESO"
                        End If
                        fac.Save()
                    End If

                End If

                detalle.MarkAsDeleted()
                detalle.Save()

            End If

            tx.CommitTransaction()

            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function Delete_Cobros_Detalle_NonTransacc(conn As String, codCobroDetalle As Integer) As Boolean

        Dim detalle As New Class_RC_Data.Class_RC.DETALLECOBROS(conn)
        detalle.Where.CodCobroDetalle.Value = codCobroDetalle

        If detalle.Query.Load Then
            'se marca la factura con estado en proceso pues se anula o borra el cobro asociado.
            'se verifica antes que no posea la venta otro cobro
            Dim detalleControlFac As New Class_RC_Data.Class_RC.DETALLECOBROS(conn)
            detalleControlFac.Where.CodVenta.Value = detalle.CodVenta
            detalleControlFac.Where.CodCobro.Value = detalle.CodCobro
            detalleControlFac.Where.CodCobro.Operator = WhereParameter.Operand.NotEqual

            If Not detalleControlFac.Query.Load Then
                Dim fac As New Class_RC_Data.Class_RC.VENTAS(conn)
                fac.Where.CodVenta.Value = detalle.CodVenta
                If fac.Query.Load Then
                    fac.TotalVentaSaldo += detalle.Monto
                    If fac.TotalVentaSaldo = fac.TotalVenta Then
                        fac.EstadoVenta = "EN PROCESO"
                    End If
                    fac.Save()
                End If

            End If

            detalle.MarkAsDeleted()
            detalle.Save()
            Return True

        Else
            Return False

        End If

    End Function

    Public Function get_Cobros_Detalles_All(conn As String, codCobro As Integer) As DataTable
        Try

            Dim vDetalle As New Class_RC_Data.Class_RC.VistaCobrosDetalle(conn)

            If codCobro > 0 Then
                vDetalle.Where.CodCobro.Value = codCobro
            End If

            'If descripcion <> "" Then
            '    vPro.Where.DescProducto.Value = "%" & descripcion & "%"
            '    vPro.Where.DescProducto.Operator = WhereParameter.Operand.Like
            'End If

            'If codTipoProducto > 0 Then
            '    vPro.Where.CodTipoProducto.Value = codTipoProducto
            'End If

            If vDetalle.Query.Load Then
                If vDetalle.DefaultView.Table.Rows.Count > 0 Then
                    Return vDetalle.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function get_formaPago(conn As String) As DataTable
        Try

            Dim vUsu As New Class_RC_Data.Class_RC.FORMAPAGO(conn)

            If vUsu.Query.Load Then
                If vUsu.DefaultView.Table.Rows.Count > 0 Then
                    Return vUsu.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function get_Imprimir_Recibo_ByID(ByVal conn As String, ByVal codCobro As Integer, ByVal codUsuario As Integer) As DataTable
        Try

            Dim vCobros As New Class_RC_Data.Class_RC.VistaCobroReciboImpresion(conn)

            If codCobro > 0 Then
                vCobros.Where.CodCobro.Value = codCobro
            End If

            If vCobros.Query.Load Then
                If vCobros.DefaultView.Table.Rows.Count > 0 Then
                    log__1.Insert_Log(conn, "Imprimio recibo, codigo: " & codCobro, codUsuario, 50, vCobros.DefaultView.Table)
                    Return vCobros.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

#End Region

#Region "Pagos"

    Public Function Insert_Pagos(conn As String, fechaPago As DateTime, codProveedor As Integer,
                                 nroRecibo As String, total As Decimal,
                                  estadoRecibo As String, codUsuario As Integer) As Integer

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim cobro As New Class_RC_Data.Class_RC.PAGOS(conn)

            cobro.AddNew()
            cobro.FechaPago = fechaPago
            cobro.CodProveedor = codProveedor

            cobro.NroRecibo = nroRecibo

            cobro.Total = total
            cobro.EstadRecibo = estadoRecibo

            cobro.CodUsuario = codUsuario
            cobro.FechaActualizacion = DateTime.Now
            cobro.Save()
            log__1.Insert_Log(conn, "Inserto registro, codigo: " & cobro.CodPago, codUsuario, 17, cobro.DefaultView.Table)
            tx.CommitTransaction()

            Return cobro.CodPago

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return 0
        End Try

    End Function

    Public Function Update_Pagos(conn As String, codPago As Integer,
                                  fechaPago As DateTime, codProveedor As Integer, nroRecibo As String, total As Decimal,
                                  estadoRecibo As String, codUsuario As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim cobro As New Class_RC_Data.Class_RC.PAGOS(conn)
            cobro.Where.CodPago.Value = codPago

            If cobro.Query.Load Then
                log__1.Insert_Log(conn, "Actualizar registro anterior, codigo: " & codPago, codUsuario, 18, cobro.DefaultView.Table)
                cobro.FechaPago = fechaPago
                cobro.CodProveedor = codProveedor
                cobro.NroRecibo = nroRecibo
                cobro.Total = total
                cobro.EstadRecibo = estadoRecibo

                cobro.CodUsuario = codUsuario
                cobro.FechaActualizacion = DateTime.Now
                cobro.Save()

            End If
            log__1.Insert_Log(conn, "Actualizar registro editado, codigo: " & codPago, codUsuario, 18, cobro.DefaultView.Table)
            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function Delete_Pagos(ByVal conn As String, ByVal codPago As Integer, ByVal codusuario As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try

            tx.BeginTransaction()

            Dim cobro As New Class_RC_Data.Class_RC.PAGOS(conn)
            cobro.Where.CodPago.Value = codPago

            If cobro.Query.Load Then

                Dim det As New Class_RC_Data.Class_RC.DETALLEPAGOS(conn)
                det.Where.CodPago.Value = codPago
                If det.Query.Load() Then
                    For i As Integer = 0 To (det.DefaultView.Table.Rows.Count - 1)
                        If Delete_Pagos_Detalle_NonTransacc(conn, det.DefaultView.Table.Rows(i)("CodPagoDetalle")) = False Then
                            Throw New Exception("Error al borrar detalles")

                        End If

                    Next

                End If
                log__1.Insert_Log(conn, "Elimino registro, codigo: " & cobro.CodPago, codusuario, 19, cobro.DefaultView.Table)
                cobro.MarkAsDeleted()
                cobro.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function get_Pagos_All(conn As String, codPago As Integer, fechaInicio As DateTime,
                                   fechaFin As DateTime, nroRecibo As String, proveedor As String,
                                   estado As String, ruc As String) As DataTable
        Try

            Dim vCobros As New Class_RC_Data.Class_RC.VistaPagos(conn)

            If codPago > 0 Then
                vCobros.Where.CodPago.Value = codPago
            End If

            If nroRecibo <> "" Then
                vCobros.Where.NroRecibo.Value = "%" & nroRecibo & "%"
                vCobros.Where.NroRecibo.Operator = WhereParameter.Operand.Like
            Else
                vCobros.Where.FechaPago.BetweenBeginValue = New DateTime(fechaInicio.Year, fechaInicio.Month, fechaInicio.Day, 0, 0, 0)
                vCobros.Where.FechaPago.BetweenEndValue = New DateTime(fechaFin.Year, fechaFin.Month, fechaFin.Day, 23, 59, 59)
                vCobros.Where.FechaPago.Operator = WhereParameter.Operand.Between
            End If

            If estado <> "" Then
                vCobros.Where.EstadoPago.Value = estado
            End If

            If proveedor <> "" Then
                vCobros.Where.Proveedor.Value = "%" + proveedor + "%"
                vCobros.Where.Proveedor.Operator = WhereParameter.Operand.Like
            End If

            If ruc <> "" Then
                vCobros.Where.Ruc.Value = "%" + ruc + "%"
                vCobros.Where.Ruc.Operator = WhereParameter.Operand.Like
            End If

            If vCobros.Query.Load Then
                If vCobros.DefaultView.Table.Rows.Count > 0 Then
                    Return vCobros.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function get_Pagos_Facturas_All(conn As String, codPago As Integer, fechaInicio As DateTime,
                                   fechaFin As DateTime, nroRecibo As String, proveedor As String,
                                   estado As String, ruc As String) As DataTable
        Try

            Dim vCobros As New Class_RC_Data.Class_RC.VistaPagos_Facturas(conn)

            If codPago > 0 Then
                vCobros.Where.CodPago.Value = codPago
            End If

            If nroRecibo <> "" Then
                vCobros.Where.NroRecibo.Value = "%" & nroRecibo & "%"
                vCobros.Where.NroRecibo.Operator = WhereParameter.Operand.Like
            Else
                vCobros.Where.FechaPago.BetweenBeginValue = New DateTime(fechaInicio.Year, fechaInicio.Month, fechaInicio.Day, 0, 0, 0)
                vCobros.Where.FechaPago.BetweenEndValue = New DateTime(fechaFin.Year, fechaFin.Month, fechaFin.Day, 23, 59, 59)
                vCobros.Where.FechaPago.Operator = WhereParameter.Operand.Between
            End If

            If estado <> "" Then
                vCobros.Where.EstadoPago.Value = estado
            End If

            If proveedor <> "" Then
                vCobros.Where.Proveedor.Value = "%" + proveedor + "%"
                vCobros.Where.Proveedor.Operator = WhereParameter.Operand.Like
            End If

            If ruc <> "" Then
                vCobros.Where.Ruc.Value = "%" + ruc + "%"
                vCobros.Where.Ruc.Operator = WhereParameter.Operand.Like
            End If

            If vCobros.Query.Load Then
                If vCobros.DefaultView.Table.Rows.Count > 0 Then
                    Return vCobros.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function get_Pagos_ByID(conn As String, codPago As Integer) As DataTable
        Try

            Dim vCobros As New Class_RC_Data.Class_RC.VistaPagos(conn)

            If codPago > 0 Then
                vCobros.Where.CodPago.Value = codPago
            End If

            If vCobros.Query.Load Then
                If vCobros.DefaultView.Table.Rows.Count > 0 Then
                    Return vCobros.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function Insert_Pagos_Detalle(conn As String, codPago As Integer, codCompra As Integer,
                                         monto As Decimal, codFormaPago As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim detalle As New Class_RC_Data.Class_RC.DETALLEPAGOS(conn)

            detalle.AddNew()
            detalle.CodPago = codPago
            detalle.CodCompra = codCompra
            detalle.Monto = monto
            detalle.CodFormaPago = codFormaPago
            detalle.Save()

            'se marca la venta con estado aprobada para evitar la edicion futura de la misma ya que posee un cobro
            Dim fac As New Class_RC_Data.Class_RC.COMPRAS(conn)
            fac.Where.CodCompra.Value = codCompra

            If fac.Query.Load Then
                fac.TotalCompraSaldo -= monto
                If fac.TotalCompraSaldo > 0 Then
                    fac.EstadoCompra = "AMORTIZADA"
                Else
                    fac.EstadoCompra = "CANCELADA"
                End If

                fac.Save()
            End If

            tx.CommitTransaction()

            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function Delete_Pagos_Detalle(conn As String, codPagoDetalle As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim detalle As New Class_RC_Data.Class_RC.DETALLEPAGOS(conn)
            detalle.Where.CodPagoDetalle.Value = codPagoDetalle

            If detalle.Query.Load Then
                'se marca la factura con estado en proceso pues se anula o borra el cobro asociado.
                'se verifica antes que no posea la venta otro cobro
                Dim detalleControlFac As New Class_RC_Data.Class_RC.DETALLEPAGOS(conn)
                detalleControlFac.Where.CodCompra.Value = detalle.CodCompra
                detalleControlFac.Where.CodPago.Value = detalle.CodPago
                detalleControlFac.Where.CodPago.Operator = WhereParameter.Operand.NotEqual

                If Not detalleControlFac.Query.Load Then
                    Dim fac As New Class_RC_Data.Class_RC.COMPRAS(conn)
                    fac.Where.CodCompra.Value = detalle.CodCompra
                    If fac.Query.Load Then
                        fac.TotalCompraSaldo += detalle.Monto
                        If fac.TotalCompraSaldo = fac.TotalCompra Then
                            fac.EstadoCompra = "EN PROCESO"
                        End If
                        fac.Save()
                    End If

                End If

                detalle.MarkAsDeleted()
                detalle.Save()

            End If

            tx.CommitTransaction()

            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function Delete_Pagos_Detalle_NonTransacc(conn As String, codPagoDetalle As Integer) As Boolean

        Dim detalle As New Class_RC_Data.Class_RC.DETALLEPAGOS(conn)
        detalle.Where.CodPagoDetalle.Value = codPagoDetalle

        If detalle.Query.Load Then
            'se marca la factura con estado en proceso pues se anula o borra el cobro asociado.
            'se verifica antes que no posea la venta otro cobro
            Dim detalleControlFac As New Class_RC_Data.Class_RC.DETALLEPAGOS(conn)
            detalleControlFac.Where.CodCompra.Value = detalle.CodCompra
            detalleControlFac.Where.CodPago.Value = detalle.CodPago
            detalleControlFac.Where.CodPago.Operator = WhereParameter.Operand.NotEqual

            If Not detalleControlFac.Query.Load Then
                Dim fac As New Class_RC_Data.Class_RC.COMPRAS(conn)
                fac.Where.CodCompra.Value = detalle.CodCompra
                If fac.Query.Load Then
                    fac.TotalCompraSaldo += detalle.Monto

                    If fac.TotalCompraSaldo = fac.TotalCompra Then
                        fac.EstadoCompra = "EN PROCESO"
                    End If

                    fac.Save()
                End If

            End If

            detalle.MarkAsDeleted()
            detalle.Save()
            Return True

        Else
            Return False

        End If

    End Function

    Public Function get_Pagos_Detalles_All(conn As String, codPago As Integer) As DataTable
        Try

            Dim vDetalle As New Class_RC_Data.Class_RC.VistaPagosDetalle(conn)

            If codPago > 0 Then
                vDetalle.Where.CodPago.Value = codPago
            End If

            'If descripcion <> "" Then
            '    vPro.Where.DescProducto.Value = "%" & descripcion & "%"
            '    vPro.Where.DescProducto.Operator = WhereParameter.Operand.Like
            'End If

            'If codTipoProducto > 0 Then
            '    vPro.Where.CodTipoProducto.Value = codTipoProducto
            'End If

            If vDetalle.Query.Load Then
                If vDetalle.DefaultView.Table.Rows.Count > 0 Then
                    Return vDetalle.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

#End Region

End Class
